﻿package bdz.projects.lavender.doi.utils
{
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.utils.*;
	/**
	 * ...
	 * @author ...
	 */
	public class DOIAudio extends EventDispatcher 
	{
		private static var instance:DOIAudio;
		private var _tick:Sound;
		private static var _ambient:Sound;
		private var _flip:Sound;
		private static var _ambientChannel:SoundChannel;
		private static var _currentChannel:SoundChannel;
		private var _ambvol:Number = .03;
		private var _effvol:Number = .1;
		private var _volon:Boolean = true;
		public var _effectOn:Boolean = true;
		public var _effdefvol:Number = .4;
		function DOIAudio(se:SingletonEnforcer):void {
			
		}
		public static function getInstance():DOIAudio {
			if (instance == null) {
				instance = new DOIAudio(new SingletonEnforcer());
				instance.initialize();
			}
			return instance;
		}
		private function initialize():void {
			try {
				var ca:* = getDefinitionByName("ClearingAmbient");
				if(ca){
					_ambient = new ca() as Sound;
				}
			}catch (e:Error) {
				
			}
		}
		public function playTick():void {
			if (_tick) {
				_currentChannel.stop();
				_tick = null;
			}
			try {
				var tr:* = getDefinitionByName("TickRoll");
				if(tr){
					_tick = new tr() as Sound;
				}
				var _trans:SoundTransform = new SoundTransform(_volon ? _effvol : 0);
				_currentChannel = _tick.play(0, 0, _trans);	
			}catch (e:Error) {
			
			}
		}
		public function playFlip():void {
			
			if (_flip) {
				_currentChannel.stop();
				_flip = null;
			}
			try {
				var pt:* = getDefinitionByName("PaperTurn");
				_flip = new pt() as Sound;
				var _trans:SoundTransform = new SoundTransform(_volon ? _effvol : 0);
				_currentChannel = _flip.play(0, 0, _trans);
			}catch(e:Error){}
		}
		public function playAmbient():void {
			try{
				var _trans:SoundTransform = new SoundTransform(_volon ? _ambvol : 0);
				try {
					_ambient.close();
				}catch (e:Error){}
				//_ambientChannel = _ambient.play(0, 9999, _trans);
			}catch(e:Error){}
		}
		public function turnOff():void {
			//_ambientChannel.stop();
			_volon = false;
		}
		public function turnOn():void {
			
			var _trans:SoundTransform = new SoundTransform(_ambvol);
			//_ambientChannel = _ambient.play(0, 9999, _trans);
			_volon = true;
		}
		public function get isOn():Boolean {
			return _volon;
		}
		private function handleEffectPlay(o:*, def:Object = null):void {
			if (def == null) {
				def = { volume:.4, delay:0, loop:0};
			}
			try{
				if (def.volume == null) {
					def.volume = .4;
				}
			}catch (e:Error) {
				def = { volume:.4, delay:0, loop:0};
				def.volume = .4;
			}
			try{
				if (def.delay == null) {
					def.delay = 0;
				}
			}catch (e:Error) {
				def = { volume:.4, delay:0, loop:0};
				def.delay = 0;
			}
			try{
				if (def.loop == null) {
					def.loop = 0;
				}
			}catch (e:Error) {
				def = { volume:.4, delay:0, loop:0};
				def.loop = 0;
			}
			try{
				var eff:Sound = new o() as Sound;
				var _trans:SoundTransform = new SoundTransform(_volon && _effectOn ? def.volume : 0);
				if (def.delay) {
					setTimeout(eff.play, def.delay * 1000, 0, 0, _trans);
				}else{
					eff.play(0, def.loop, _trans);
				}
			}catch(e:Error){}

		}
		public function playEffect(o:*, def:Object = null):void {
			setTimeout(handleEffectPlay, 50, o, def);
						
		}
	}
	
}
/**
 * @private
 * 
 */
internal class SingletonEnforcer{}